package com.cy.pj.common.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Component
@Aspect
public class CacheAspect { //系统底层会将切面中的内容转换成AdviSor对象
    private Map<String,Object> cache = new ConcurrentHashMap<>();

    @Pointcut("bean(moduleServiceImpl)")
    public void doCache(){};

    @Around("doCache()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        //1.取数据
        Object obj = cache.get("userPer");
        if(obj!=null){
            return obj;
        }
        //2.cache没有则查数据
        obj = joinPoint.proceed();
        //3.将数据存储到cache
        cache.put("userPer", obj);
        return obj;
    }
}
